package cn.itcast.hotel.test;

import cn.itcast.hotel.pojo.Hotel;
import cn.itcast.hotel.pojo.HotelDoc;
import cn.itcast.hotel.service.IHotelService;
import com.alibaba.fastjson.JSON;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.List;

@SpringBootTest
public class HotelDemo {
    private RestHighLevelClient client = null;

    @Autowired
    private IHotelService service;

    /**
     * 查询mysql中的数据信息
     */
    @Test
    public void testFindById(){
        Hotel hotel = service.getById("36934");
//        System.out.println(hotel);
        // 转成json字符串
        String hotelJson = JSON.toJSONString(hotel);
        System.out.println(hotelJson);
    }

    // TODO:需求: 将mysql中的数据转存放到ES中
    /*
        1.创建索引库和映射关系
        2.从mysql中查询数据信息并转成json
        3.将数据添加到ES中
     */
    // 1.创建索引库和映射关系
    @Test
    public void createHotelIndex() throws IOException {
        //a.创建请求语义对象
        CreateIndexRequest request = new CreateIndexRequest("hotel");
        //设置请求参数
        request.mapping(RequestDsl.HOTELDSL,XContentType.JSON);
        //b.发送请求
        CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
        //c.处理响应结果
        System.out.println(response.isAcknowledged());
    }

    @Test
    public void testDataMysqlToEs() throws IOException {
        // 与数据库表对应的实体
        Hotel hotel = service.getById(36934L);
        // TODO:将hotel转成HotelDoc(与ES库对应)
        HotelDoc hotelDoc = new HotelDoc(hotel);
        // 转成json字符串
        String hotelJson = JSON.toJSONString(hotelDoc);
        System.out.println(hotelJson);
        //====================向Es中添加数据信息
        //创建请求语义对象
        IndexRequest request = new IndexRequest("hotel").id(hotelDoc.getId()+"");
        // 设置数据信息
        request.source(hotelJson,XContentType.JSON);
        //发送请求
        IndexResponse response = client.index(request, RequestOptions.DEFAULT);
        System.out.println(response.getResult());
    }

    @Test
    public void testDataMysqlToEsAll() throws IOException {
        // 查询所有酒店信息
        List<Hotel> hotels = service.list();
        // 批量添加文档数据
        BulkRequest request = new BulkRequest();
        for (Hotel hotel : hotels) {
            HotelDoc hotelDoc = new HotelDoc(hotel);
            // 封装每一个doc对象
            IndexRequest indexRequest = new IndexRequest("hotel").id(hotelDoc.getId()+"");
            indexRequest.source(JSON.toJSONString(hotelDoc),XContentType.JSON);
            // 追加到bulk中
            request.add(indexRequest);
        }
        BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
        System.out.println(response.status());
    }



    /**
     * 创建ES的客户端对象
     */
    @BeforeEach
    public void init(){
        client = new RestHighLevelClient(RestClient.builder(
                new HttpHost("192.168.11.129", 9200, "http"))
        );
//        client = new RestHighLevelClient(RestClient.builder(
//                HttpHost.create("http://192.168.190.151:9200")
//        ));
        //System.out.println(client);
    }

    /**
     * 关闭ES客户端对象
     * @throws IOException
     */
    @AfterEach
    public void destory() throws IOException {
        if (client!=null){
            client.close();
        }
    }
}
